package com.tool.btn;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.util.List;

import org.come.Frame.AircraftJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.AircraftJPanel;
import org.come.Jpanel.ZhuJpanel;
import org.come.bean.RoleShow;
import org.come.entity.Fly;
import org.come.entity.Goodstable;
import org.come.mouslisten.GoodsMouslisten;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.*;

import com.tool.image.ImageMixDeal;
import com.tool.image.ManimgAttribute;
//飞行器
public class AircraftBtn extends MoBanBtn {
    private int caozuo;
    private AircraftJPanel aircraftJPanel;
    private ZhuJpanel zhuJpanel;

    public AircraftBtn(String iconpath, int type, int caozuo, AircraftJPanel aircraftJPanel) {
        super(iconpath, type);
        this.caozuo = caozuo;
        this.aircraftJPanel = aircraftJPanel;
    }

    public AircraftBtn(String iconpath, int type, String text, Font font, Color[] colors, int caozuo, AircraftJPanel aircraftJPanel) {
        super(iconpath, type, colors);
        this.setText(text);
        this.setFont(font);
        this.setVerticalTextPosition(0);
        this.setHorizontalTextPosition(0);
        this.caozuo = caozuo;
        this.aircraftJPanel = aircraftJPanel;
    }


    public AircraftBtn(String iconpath, int type, String text, Font font, Color[] colors, int caozuo, ZhuJpanel zhuJpanel) {
        super(iconpath, type, colors);
        this.setText(text);
        this.setFont(font);
        this.setVerticalTextPosition(0);
        this.setHorizontalTextPosition(0);
        this.caozuo = caozuo;
        this.zhuJpanel = zhuJpanel;
    }



    public void chooseyes() {
    }

    public void chooseno() {
    }

    public void nochoose(MouseEvent e) {
        if (this.caozuo == 0) {
            this.getGoodType(1101L);
        } else if (this.caozuo == 1) {
            this.getGoodType(1102L);
        } else if (this.caozuo == 2) {
            this.getGoodType(1103L);
        } else if (this.caozuo == 3) {
            if (this.aircraftJPanel.getFlyId() == null) {
                ZhuFrame.getZhuJpanel().addPrompt2("请选择一个飞行器");
                return;
            }
            if (ImageMixDeal.userimg.getRoleShow().getBooth_id() != null) {
                ZhuFrame.getZhuJpanel().addPrompt2("摆摊状态不可以飞行！");
                return;
            }
            rideOrReset();

        } else if (this.caozuo == 4) {
            this.getGoodType(1104L);
        } else if (this.caozuo == 5) {
            ZhuFrame.getZhuJpanel().addPrompt2("暂未开放！");
            return;
//            if (this.aircraftJPanel.getFlyId() != null) {
//                Fly flyOne = RoleData.getRoleData().getFlyOne(this.aircraftJPanel.getFlyId());
//                NPCDialog npcDialog = new NPCDialog();
//                npcDialog.setMsg("花费燃灵值探查地煞星位置");
//                List functions = new ArrayList();
//                StringBuffer buffer = new StringBuffer();

               /* for (int i = 0; i < flyOne.getStairs(); ++i) {
                    buffer.setLength(0);
                    buffer.append("查询");
                    buffer.append(11 + i);
                    buffer.append("星位置(扣除");
                    buffer.append(10 * (1 + i));
                    buffer.append("点燃灵值)");
                    functions.add(buffer.toString());
                }*/

//                buffer = null;
//                npcDialog.setFunctions(functions);
//                NPCJfram.getNpcJfram().getNpcjpanel().npc(npcDialog);
//            }
        } else {
            AircraftBtn[] aircraftBtns;
            int i;
            if (this.caozuo == 10) {
                String sendmes = null;

                try {
                    sendmes = Agreement.getAgreement().MountAgreement();
                } catch (Exception var7) {
                    var7.printStackTrace();
                }


                sendmes = Agreement.getAgreement().MountAgreement();
                SendMessageUntil.toServer(sendmes);
                ZhuFrame.getZhuJpanel().showflybtn(true,1);
                aircraftBtns = this.zhuJpanel.getAircraftBtns();
                Music.addyinxiao("开关窗口.mp3");

                for (i = 0; i < aircraftBtns.length; ++i) {
                    aircraftBtns[i].setVisible(false);
                }
            } else if (this.caozuo == 11) {
              /*  List flys = RoleData.getRoleData().getFly();
                if (flys != null) {
                   if (flys.size() == 0) {
                       ZhuFrame.getZhuJpanel().addPrompt2("你还没有飞行器，请先获取");
                        return;
                    }*/

                   /* RoleData.getRoleData().getLoginResult().setFly_id(new BigDecimal("1"));
                    Fly fly = new Fly();
                    fly.setExp(9999);
                    fly.setFid(new BigDecimal("1"));
                    fly.setName(fly.getName());
                    fly.setSkin("104");
                    fly.setSkill("8055");*/
                /*    AircraftJframe.getAircraftJframe().getAircraftJPanel().initShowView((Fly) flys.get(0));
//                    AircraftJframe.getActivityJframe().getAircraftJPanel().initShowView((Fly) flys.get(0));
                }*/
                String sendmes = null;



                List<Fly> flys;
                sendmes = Agreement.getAgreement().FlyAgreement();
                SendMessageUntil.toServer(sendmes);
                ZhuFrame.getZhuJpanel().showflybtn(true,1);
                // AircraftJframe.getAircraftJframe().getAircraftJPanel().initShowView((Fly) flys.get(0));
                // FormsManagement.showForm(119);
                aircraftBtns = this.zhuJpanel.getAircraftBtns();
                Music.addyinxiao("开关窗口.mp3");

                for (i = 0; i < aircraftBtns.length; ++i) {
                    aircraftBtns[i].setVisible(false);
                }
            } else if (this.caozuo == 12) {

//                String sendmes = null;
//
//                List<Fly> flys;
//                sendmes = Agreement.getAgreement().FlyAgreement();
//                SendMessageUntil.toServer(sendmes);
                ZhuFrame.getZhuJpanel().settingsMenu(true,1);
                aircraftBtns = this.zhuJpanel.getAircraftBtns();

                for (i = 0; i < aircraftBtns.length; ++i) {
                    aircraftBtns[i].setVisible(false);
                }
            } else if (this.caozuo == 13) {

//                String sendmes = null;
//
//                List<Fly> flys;
//                sendmes = Agreement.getAgreement().FlyAgreement();
//                SendMessageUntil.toServer(sendmes);
                ZhuFrame.getZhuJpanel().tradingMenu(true,1);
                aircraftBtns = this.zhuJpanel.getAircraftBtns();

                for (i = 0; i < aircraftBtns.length; ++i) {
                    aircraftBtns[i].setVisible(false);
                }
            }
        }

    }
    //飞行待机
    public void rideOrReset() {

        AircraftJPanel aircraftJPanel= AircraftJframe.getAircraftJframe().getAircraftJPanel();
        RoleShow roleShow=ImageMixDeal.userimg.getRoleShow();

        if (roleShow.getMapid().intValue() == 1732 || roleShow.getMapid().intValue() == 1761) {
            ZhuFrame.getZhuJpanel().addPrompt2("当前地图不可以飞行");
            return;
        }
        if (ImageMixDeal.userimg.getRoleShow().getBooth_id() != null) {
            ZhuFrame.getZhuJpanel().addPrompt2("摆摊状态不可以飞行！");
            return;
        }
        // if (ImageMixDeal.userimg.getRoleShow().getMount_id() != 0) {
        //  ZhuFrame.getZhuJpanel().addPrompt2("请先下了坐骑，要不然怕你摔下来！");
        //  return;
        //  }
//         if (roleShow.getMount_id()!=0)
//           return;
        if (aircraftJPanel.getlistfly().getSelectedValue() != null) {
            int index = aircraftJPanel.getlistfly().getSelectedIndex();
            Fly fly = ZhuJpanel.getListFly().get(index);
            if(fly.getFuel()<=0) {
                ZhuFrame.getZhuJpanel().addPrompt2("然灵值不足，无法飞行，请补充！！！");
                return;
            }
            if (aircraftJPanel.getBtnFight().getText().equals("飞行")) {
                if (roleShow.getFly_id() !=0) {
                    aircraftJPanel.getmodelfly().set(index,"( * ) "+aircraftJPanel.getmodelfly().get(index));
                    for (int i = 0; i < ZhuJpanel.getListFly().size(); i++)
                        if (ZhuJpanel.getListFly().get(i).getFlytid() == roleShow.getFly_id()){
                            aircraftJPanel.getmodelfly().set(i,ZhuJpanel.getListFly().get(i).getFlyname());
                            break;
                        }
                    roleShow.setFly_id(ZhuJpanel.getListFly().get(index).getFlytid());
                } else {
                    aircraftJPanel.getmodelfly().set(index,"( * ) "+aircraftJPanel.getmodelfly().get(index));
                    roleShow.setFly_id(ZhuJpanel.getListFly().get(index).getFlytid());
                }
                ExpIncreaseUntil.ShouFlyValue(ZhuJpanel.getListFly().get(index));
                ManimgAttribute.flyskin=this.aircraftJPanel.getSkin();
                ManimgAttribute.ISFLY=true;
                ManimgAttribute.IS=true;
                ImageMixDeal.userimg.changeskin(null);
                Util.oldFly_id = roleShow.getFly_id();
                Util.oldFlyIndex = index;
                String mes = Agreement.getAgreement().rolechangeAgreement("F"+roleShow.getFly_id()+"="+ZhuJpanel.getListFly().get(index).getMid()+"="+this.aircraftJPanel.getSkin());
                SendMessageUntil.toServer(mes);
                AircraftJframe.getAircraftJframe().getaircraftJPanel().getBtnFight().setText("待机");
                FormsManagement.HideForm(119);// 隐藏窗体
            } else if (AircraftJframe.getAircraftJframe().getaircraftJPanel().getBtnFight().getText().equals("待机")) {
                AircraftJframe.getAircraftJframe().getaircraftJPanel().getmodelfly().set(index,ZhuJpanel.getListFly().get(index).getFlyname());
                roleShow.setFly_id(0);
                ManimgAttribute.isdaiji=true;
//                ManimgAttribute.Mythread mythread=new ManimgAttribute.Mythread();
                ImageMixDeal.userimg.changeskin(null);
                ManimgAttribute.ISFLY=false;
                // mythread.start();
                String mes = Agreement.getAgreement().rolechangeAgreement("F");
                SendMessageUntil.toServer(mes);
                AircraftJframe.getAircraftJframe().getaircraftJPanel().getBtnFight().setText("飞行");
                FormsManagement.HideForm(119);
            }
        } else {
            ZhuFrame.getZhuJpanel().addPrompt2("请选择您要飞行/待机的飞行器！");
        }
    }
    public void getGoodType(long type) {
        Goodstable[] goodslist = GoodsListFromServerUntil.getGoodslist();
        Goodstable goodstable = null;
        int i = 0;

        for (int len = goodslist.length; i < len; ++i) {
            if (goodslist[i] != null && goodslist[i].getType() == type) {
                goodstable = goodslist[i];
                break;
            }
        }

        if (goodstable == null) {
            ZhuFrame.getZhuJpanel().addPrompt2("背包里没有对应物品,请先获取");
        } else {
            for(int l=ZhuJpanel.getListFly().size()-1;l>=0;l--){
                Fly fly=ZhuJpanel.getListFly().get(l);
                String flyname = "";
                if(fly.getFlyname()!=null) {
                    flyname = fly.getFlyname()+"("+AircraftJPanel.getJieshu(fly.getFlystate())+"阶)";
                }
                if(fly.getFlystate()>=5 && type!=1103) {
                    ZhuFrame.getZhuJpanel().addPrompt2("飞行器#G【"+fly.getFlyname()+"】#Y已达到最高阶#G"+AircraftJPanel.getJieshu(fly.getFlystate())+"#Y阶！");
                }else
                if (flyname.equals(this.aircraftJPanel.getLabName().getText())) {
                    GoodsMouslisten.useFly(goodstable,fly);
                    ExpIncreaseUntil.ShouFlyValue(fly);
                }//
            }
        }
    }

    //飞行器
    public static void quickFly() {
        RoleShow roleShow=ImageMixDeal.userimg.getRoleShow();
        int index =  Util.oldFlyIndex;
        Fly fly = ZhuJpanel.getListFly().get(index);
        if(fly.getFuel()<=0) {
            ZhuFrame.getZhuJpanel().addPrompt2("然灵值不足，无法飞行，请补充！！！");
            return;
        }
        if (ImageMixDeal.userimg.getRoleShow().getBooth_id() != null) {
            ZhuFrame.getZhuJpanel().addPrompt2("摆摊状态不可以飞行！");
            return;
        }
        //  if (ImageMixDeal.userimg.getRoleShow().getMount_id() != 0) {
        //  ZhuFrame.getZhuJpanel().addPrompt2("请先下了坐骑，要不然怕你摔下来！");
        //    return;
        // }
        String stste = AircraftJframe.getAircraftJframe().getaircraftJPanel().getBtnFight().getText();
        if(stste.equals("飞行")) {
            roleShow.setFly_id(Util.oldFly_id);
            if (roleShow.getFly_id() !=0) {
                roleShow.setFly_id(ZhuJpanel.getListFly().get(index).getFlytid());
            } else {
                roleShow.setFly_id(ZhuJpanel.getListFly().get(index).getFlytid());
            }
            ExpIncreaseUntil.ShouFlyValue(ZhuJpanel.getListFly().get(index));
            ManimgAttribute.flyskin = ZhuJpanel.getListFly().get(index).getSkin();
            ManimgAttribute.ISFLY=true;
            ManimgAttribute.IS=true;
            ImageMixDeal.userimg.changeskin(null);
            String mes = Agreement.getAgreement().rolechangeAgreement("F"+roleShow.getFly_id()+"="+ZhuJpanel.getListFly().get(index).getMid()+"="+ZhuJpanel.getListFly().get(index).getSkin());
            SendMessageUntil.toServer(mes);
            AircraftJframe.getAircraftJframe().getaircraftJPanel().getBtnFight().setText("待机");
        }else if(stste.equals("待机")) {
            roleShow.setFly_id(0);
            ManimgAttribute.isdaiji=true;
            ImageMixDeal.userimg.changeskin(null);
            String mes = Agreement.getAgreement().rolechangeAgreement("F");
            SendMessageUntil.toServer(mes);
            AircraftJframe.getAircraftJframe().getaircraftJPanel().getBtnFight().setText("飞行");
            ManimgAttribute.ISFLY=false;
        }
    }


}
